package fr.cie.blog.modeldao.hib;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import fr.cie.blog.beans.Card;
import fr.cie.blog.beans.Interest;
import fr.cie.blog.domainservice.IServiceInterest;

/**
 * @author Jee3
 * 
 */
public class InterestDaoImplHib extends HibernateDaoSupport implements
		IServiceInterest {
	/**
	 * Log4J the logger.
	 */
	private static final Logger LOG = Logger
			.getLogger(InterestDaoImplHib.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#save(java.lang.Object)
	 */
	@Override
	public final int save(final Interest interest) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(interest);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update Interest error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#update(java.lang.Object)
	 */
	@Override
	public final int update(final Interest interest) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.saveOrUpdate(interest);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update Interest error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#delete(java.lang.Object)
	 */
	@Override
	public final int delete(final Interest interest) {
		final Session hibSession = getSessionFactory().getCurrentSession();
		final Transaction transaction = hibSession.beginTransaction();

		try {
			hibSession.delete(interest);
			transaction.commit();

		} catch (HibernateException e) {
			transaction.rollback();
			LOG.error("update Interest error", e);

		} finally {
			getSession().close();
		}
		return 1;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findAll()
	 */
	@Override
	public final List<Interest> findAll() {
		@SuppressWarnings("unchecked")
		final List<Interest> list = (List<Interest>) getHibernateTemplate()
				.find("from Interest");
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * fr.cie.blog.domainservice.IServiceInterest#findByCard(fr.cie.blog.beans
	 * .Card)
	 */
	@Override
	public final List<Interest> findByCard(final Card pCard) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see fr.cie.blog.domainservice.IServiceGeneric#findById(int)
	 */
	@Override
	public final Interest findById(final long pIdentifier) {
		return getHibernateTemplate().get(Interest.class, pIdentifier);
	}

	@Override
	public final List<Interest> findById(final List<Integer> pIdList) {
		String query = "from Interest as i where";
		for (Integer interestId : pIdList) {
			query = query.concat("i.id = " + interestId + " or ");
		}
		// suppress the last " or " substring
		query = query.substring(0, query.length() - 4);

		@SuppressWarnings("unchecked")
		final List<Interest> interestList = 
			(List<Interest>) getHibernateTemplate().find(query);

		return interestList;
	}
}